/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author alumneDAM
 */
public class ConexionBD {

    private Connection con;

    public boolean conexion() throws SQLException {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //Connexió externa
            con = DriverManager.getConnection(
                    "jdbc:oracle:thin:@ieslaferreria.xtec.cat:8081:BD1314",
                    "TRIVIAL", "TRIVIAL");
            System.out.println("Connexio EXTERNA OK!");
        } catch (ClassNotFoundException ex) {
            System.out.println("Controlador OracleDriver no trobat ");
            return false;
        } catch (SQLException ex1) {
            System.out.println("Error en la connexió INTERNA amb la base de dades.");

            try {
                //Connexió interna, des del centre
                con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.180.10:1521:BD1314",
                        "TRIVIAL", "TRIVIAL");
                System.out.println("Connexio INTERNA OK!");

            } catch (SQLException ex2) {
                System.out.println("Error en la connexió  amb la base de dades.");
                return false;
            }
        }
        return true;
    }

    public boolean cerrarConexion() {
        boolean comprova = false;

        try {
            if (con != null && !con.isClosed()) {
                con.close();
                comprova = true;
                System.out.println("Conexion Cerrada");
            }
        } catch (SQLException ex) {
            System.out.println("Error " + ex.getMessage());
        } finally {
            return comprova;
        }
    }

    public Connection getCon() {
        return con;
    }

}
